package com.hzsparrow.ai.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzsparrow.ai.entity.PlatformSettings;
import com.hzsparrow.ai.mapper.PlatformSettingsMapper;
import com.hzsparrow.ai.config.AIModelInitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 平台设置服务实现类
 */
@Service
public class PlatformSettingsServiceImpl extends ServiceImpl<PlatformSettingsMapper, PlatformSettings> implements PlatformSettingsService {

    @Autowired
    private AIModelInitService aiModelInitService;

    @Override
    public PlatformSettings getPlatformSettings() {
        // 查询第一条设置记录，如果不存在则创建默认设置
        LambdaQueryWrapper<PlatformSettings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.last("LIMIT 1");
        PlatformSettings settings = this.getOne(queryWrapper);
        
        if (settings == null) {
            settings = initDefaultSettings();
        }
        
        return settings;
    }

    @Override
    public PlatformSettings updatePlatformSettings(PlatformSettings settings) {
        // 获取现有设置
        PlatformSettings existing = getPlatformSettings();
        
        // 记录旧的模型ID，用于比较是否需要更新
        Long oldChatModelId = existing.getDefaultChatModelId();
        Long oldEmbeddingModelId = existing.getDefaultEmbeddingModelId();
        
        if (existing.getId() != null) {
            // 更新现有设置
            settings.setId(existing.getId());
            settings.setCreateTime(existing.getCreateTime());
            settings.setUpdateTime(LocalDateTime.now());
            this.updateById(settings);
        } else {
            // 创建新设置
            LocalDateTime now = LocalDateTime.now();
            settings.setCreateTime(now);
            settings.setUpdateTime(now);
            this.save(settings);
        }
        
        // 检查对话模型是否发生变化，如果是则实时更新
        if (!java.util.Objects.equals(oldChatModelId, settings.getDefaultChatModelId())) {
            if (settings.getDefaultChatModelId() != null) {
                aiModelInitService.updateDefaultChatModel(settings.getDefaultChatModelId());
            }
        }
        
        // 检查嵌入模型是否发生变化，如果是则实时更新
        if (!java.util.Objects.equals(oldEmbeddingModelId, settings.getDefaultEmbeddingModelId())) {
            if (settings.getDefaultEmbeddingModelId() != null) {
                aiModelInitService.updateDefaultEmbeddingModel(settings.getDefaultEmbeddingModelId());
            }
        }
        
        return settings;
    }

    @Override
    public PlatformSettings initDefaultSettings() {
        PlatformSettings settings = new PlatformSettings();
        LocalDateTime now = LocalDateTime.now();
        settings.setCreateTime(now);
        settings.setUpdateTime(now);
        
        // 保存默认设置
        this.save(settings);
        return settings;
    }
} 